#include "goodsinput.h"
#include "ui_goodsinput.h"

GoodsInput::GoodsInput(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GoodsInput)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());
    //初始化combo box
    InitComboBoxFunc();

}

GoodsInput::~GoodsInput()
{
    delete ui;
}

void GoodsInput::on_pushButton_enter_clicked()    //商品入库
{
    if(ui->lineEdit->text().isEmpty())
    {
        QMessageBox::information(this,"error","请填写数量");
        return;
    }


    QString comboText = ui->comboBox->currentText();
    comboText.remove(QChar('\n')); // 去除换行符，可按需添加其他特殊字符过滤
    int id = comboText.toInt();

    QString lineText = ui->lineEdit->text();
    lineText.remove(QChar('\n'));
    int addamout = lineText.toInt();



    QSqlQuery query;
    QString sql = "update stockdatatable set stockAmout=stockAmout+? where stockId=?";
    query.prepare(sql);
    query.addBindValue(addamout);
    query.addBindValue(id);

    if (!query.exec())
    {
        QMessageBox::information(this,"tip","sql失败"+query.lastError().text());
        return;
    }

    ui->lineEdit->clear();
    QMessageBox::information(this,"tip","入库成功");
    emit warehouseSucceeded();

}

void GoodsInput::on_pushButton_exit_clicked()   //退出
{
    close();
}

void  GoodsInput::InitComboBoxFunc()     //对下拉框进行初始化
{
    int i=0;
    QString strid;
    QSqlQuery query;
    QString str="select *from stockdatatable";
    query.exec(str);
    while(query.next())
    {
        strid=query.value(0).toString();
        ui->comboBox->insertItem(i,strid);
        i++;

    }
}



//
